#include "QuadRenderer.h"
#include "2DDevice.h"
#include "Window.h"
#include <d3dx9.h>
#include "Map.h"
#include "Main.h"

cQuadRenderer::cQuadRenderer(cMain & Main): Main(Main)
{
	g_pVB = NULL;
	NumQuadsToRender = 0;
	Restart();
}


cQuadRenderer::~cQuadRenderer()
{

}

void cQuadRenderer::Restart()
{
	NumQuadsToRender = 0;
    if( g_pVB != NULL )
	{
        g_pVB->Release();
		g_pVB = NULL;
	}

	if( FAILED( Main.GetTwoDDevice().GetDevice()->CreateVertexBuffer(MAX_QUADS_TO_RENDER * NUM_VERTICES_IN_QUAD * sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
		MessageBox(0, "cQuadRenderer::CreateVertexBuffer failed", WINDOW_CAPTION,MB_OK); 
        return ;
    }
}



void cQuadRenderer::AddQuadToRender(D3DXVECTOR3 Pos, DWORD color, float QSize)
{
   // memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
	if(NumQuadsToRender == MAX_QUADS_TO_RENDER)
	{
		MessageBox(0, "cQuadRenderer::AddQuadToRender failed   ____NumQuadsToRender == MAX_QUADS_TO_RENDER___ ", WINDOW_CAPTION,MB_OK); 
        return;
	}
	Quads[NumQuadsToRender].pos = Pos;
	Quads[NumQuadsToRender].color = color;
	Quads[NumQuadsToRender].QSize = QSize;
	NumQuadsToRender++;
}

void cQuadRenderer::Render()
{
	int i;
	float QSize;
 CUSTOMVERTEX* pVertices;
    if( FAILED( g_pVB->Lock( 0, NUM_VERTICES_IN_QUAD * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) )
	{
		MessageBox(0, "cQuadRenderer::g_pVB->Lock failed", WINDOW_CAPTION,MB_OK); 
        return ;
	}
			for(i=0; i!= NumQuadsToRender; i++)
			{
				QSize = Quads[i].QSize;
				pVertices[ 6*i   ].pos = Main.GetMap().GetStartPos() + Quads[i].pos;
				pVertices[(6*i)+1].pos = Main.GetMap().GetStartPos() + Quads[i].pos + D3DXVECTOR3( QSize, 0.0f, 0.0f);
				pVertices[(6*i)+2].pos = Main.GetMap().GetStartPos() + Quads[i].pos + D3DXVECTOR3(QSize, -QSize, 0.0f);

				pVertices[(6*i)+3].pos = Main.GetMap().GetStartPos() + Quads[i].pos;
				pVertices[(6*i)+4].pos = Main.GetMap().GetStartPos() + Quads[i].pos + D3DXVECTOR3(QSize, -QSize, 0.0f);
				pVertices[(6*i)+5].pos = Main.GetMap().GetStartPos() + Quads[i].pos + D3DXVECTOR3(0.0f, -QSize, 0.0f);
				
				pVertices[(6*i)  ].color = Quads[i].color;
				pVertices[(6*i)+1].color = Quads[i].color;
				pVertices[(6*i)+2].color = Quads[i].color;
				pVertices[(6*i)+3].color = Quads[i].color;
				pVertices[(6*i)+4].color = Quads[i].color;
				pVertices[(6*i)+5].color = Quads[i].color;
			}
	g_pVB->Unlock();
	Main.GetTwoDDevice().GetDevice()->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    
	D3DXMATRIX matWorld;
	D3DXMatrixIdentity(&matWorld);
   Main.GetTwoDDevice().GetDevice()->SetTransform( D3DTS_WORLD, &matWorld );

    
    D3DXMATRIX matView;
    D3DXMatrixIdentity(&matView);
    Main.GetTwoDDevice().GetDevice()->SetTransform( D3DTS_VIEW, &matView );
	
	Main.GetTwoDDevice().GetDevice()->DrawPrimitive( D3DPT_TRIANGLELIST, 0,  NumQuadsToRender * NUM_TRIANGLES_IN_QUAD);

	NumQuadsToRender = 0;
}